﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using 立库托盘入库.HttpUntil;
using 立库托盘入库.MyInterface;
using 立库托盘入库.ViewModel;

namespace 立库托盘入库
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window, LoginInterface
    {
        NameViewModel _viewModel;
        public static String path = "";
        public static String token = "";
        public static String COOKIE = "";
        ProgressDialog Pd;
        Boolean isdenglu = false;
        public static CookieContainer cookiecon = new CookieContainer();
        public Login()
        {
            InitializeComponent();
            path = AppDomain.CurrentDomain.BaseDirectory;
            _viewModel = base.DataContext as NameViewModel;
            read_account();
        }

        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            if (isdenglu)
            {
                return;
            }
            login login = new login();
            login.username = _viewModel.UserName;
            login.password = Password.Password;
            _viewModel.PassWord = Password.Password;
            string json = JsonConvert.SerializeObject(login);
            isdenglu = true;
            MyHttpUtil.HttpDenglu(this, json);
            _viewModel.IsSampleDialogOpen = Visibility.Visible;
            //Pd = new ProgressDialog();
            //Pd.ShowDialog();
            //Post(rul, json);
        }

        private void write_account()
        {
            string filePath = path + "save\\Login.xml";
            XElement car = new XElement("Login",
                             new XAttribute("user_name", _viewModel.UserName),
                             new XAttribute("password", _viewModel.PassWord));
            car.Save(filePath);
        }

        private void read_account()
        {
            if (Directory.Exists(path + "save") == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path + "save");
            }
            if (File.Exists(path + "save\\Login.xml"))
            {
                string filePath = path + "save\\Login.xml";
                XElement xmlDoc = XElement.Load(filePath);
                _viewModel.UserName = xmlDoc.Attribute("user_name").Value;
                _viewModel.PassWord = xmlDoc.Attribute("password").Value;
                Password.Password = _viewModel.PassWord;
            }
            else
            {
                //不存在文件
                //Directory.CreateDirectory(path + "save\\Login.xml");//创建该文件
                string filePath = path + "save\\Login.xml";
                XElement car = new XElement("Login",
                                 new XAttribute("user_name", ""),
                                 new XAttribute("password", ""));
                car.Save(filePath);

            }

        }
        /**登录成功*/
        public void HttpLoginOk(string data, HttpWebResponse resp)
        {
            _viewModel.IsSampleDialogOpen = Visibility.Hidden;
            var json2 = (JObject)JsonConvert.DeserializeObject(data);

            String session_name = (String)json2["session_name"];
            String cookie = "";
            token = (String)json2["token"];
            //cookiecon.Add(resp.Cookies);
            foreach (Cookie cook in resp.Cookies)
            {

                if (cook.Name == session_name)
                {
                    cookie = cook.Value;
                    COOKIE = session_name + "=" + cook.Value;
                }
            }
            write_account();

            //string filePath = path + "save\\Login.xml";
            //Console.WriteLine(filePath);
            //XElement car = new XElement("Login",
            //                 new XAttribute("session_name", cookie),
            //                 new XAttribute("token", token),
            //                 new XAttribute("cookie", cookie));
            //car.Save(filePath);

            MainWindow Mn = new MainWindow();
            Mn.Show();
            Window window = Window.GetWindow(this);//关闭父窗体
            window.Close();
        }

        public void HttpLoginNo(string e, bool b)
        {
            isdenglu = false;
            _viewModel.IsSampleDialogOpen = Visibility.Hidden;
            MessageBox.Show(e);
        }
    }
    class login
    {
        public string username { get; set; }
        public string password { get; set; }
    }
}
